﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Excel;
using log4net.Appender;
using Telerik.Web.UI;

public partial class StudentEmailHistory : BaseReportPage
{
	public override string Icon
	{
		get
		{
			return "images/report_icon.png";
		}
	}

	private bool _isExporting;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			Fill();
		}
	}


	protected void lbExport_Click(object sender, ImageClickEventArgs e)
	{
		//_isExporting = true;
		//ExportToExcel("StudentEmailHistory", rgStudentEmailHistory);

		Fill();

		DataTable dt = rgStudentEmailHistory.DataSource as DataTable;

		if (dt != null)
		{
			int startRow = 1;
			string[] listFields = new string[]
			                      	{
			                      		"DELIVERY_DATE", "BANV_STATUS", "PERSONAL_STATUS", "PARENT_STATUS"
			                      	};
			Excel ex = Utils.GridToExcel(dt, listFields, "StudentEmailHistory", false, ref startRow);
			Worksheet ws = ex.Worksheets[0];
			for (int i = 1; i < startRow - 1; ++i)
			{
				DataRow row = dt.Rows[i - 1];
				for (int j = 0; j < listFields.Length; ++j)
				{
					int dtColumn = j + 2;
					if (row.Table.Columns[dtColumn].ColumnName == "BANV_STATUS")
					{
						string iStatusBanv = row["BANV_STATUS"] is DBNull ? null : row["BANV_STATUS"].ToString();
						if (iStatusBanv == EmailDeliveryStatus.Failed.ToString())
							ws.Cells[i, j].Style.ForegroundColor = Color.Red;
						else if (iStatusBanv == EmailDeliveryStatus.Delivered.ToString())
							ws.Cells[i, j].Style.ForegroundColor = Color.Green;

						ws.Cells[i, j].Style.Font.Color = Color.White;
					}

					if (row.Table.Columns[dtColumn].ColumnName == "PERSONAL_STATUS")
					{
						string iStatusBanv = row["PERSONAL_STATUS"] is DBNull ? null : row["PERSONAL_STATUS"].ToString();
						if (iStatusBanv == EmailDeliveryStatus.Failed.ToString())
							ws.Cells[i, j].Style.ForegroundColor = Color.Red;
						else if (iStatusBanv == EmailDeliveryStatus.Delivered.ToString())
							ws.Cells[i, j].Style.ForegroundColor = Color.Green;

						ws.Cells[i, j].Style.Font.Color = Color.White;
					}

					if (row.Table.Columns[dtColumn].ColumnName == "PARENT_STATUS")
					{
						string iStatusBanv = row["PARENT_STATUS"] is DBNull ? null : row["PARENT_STATUS"].ToString();
						if (iStatusBanv == EmailDeliveryStatus.Failed.ToString())
							ws.Cells[i, j].Style.ForegroundColor = Color.Red;
						else if (iStatusBanv == EmailDeliveryStatus.Delivered.ToString())
							ws.Cells[i, j].Style.ForegroundColor = Color.Green;

						ws.Cells[i, j].Style.Font.Color = Color.White;
					}

					ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].Color = Color.Black;
					ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

					ws.Cells[i, j].Style.Borders[BorderType.RightBorder].Color = Color.Black;
					ws.Cells[i, j].Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

					ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].Color = Color.Black;
					ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

					ws.Cells[i, j].Style.HorizontalAlignment = TextAlignmentType.Center;
				}

				//ws.Cells[i, 12].Style.Pattern = BackgroundType.Solid;
				//ws.Cells[i, 12].Style.ForegroundColor = Color.Gray;
			}



			ex.Save("StudentEmailHistory Download.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Page.Response);
			Page.Response.End();
		}
	}

	protected void reportGrid_ItemCreated(object sender, GridItemEventArgs e)
	{
		RadGrid grid = sender as RadGrid;

		if (e.Item is GridDataItem)
		{
			DataRowView drv = e.Item.DataItem as DataRowView;
			
			if (drv != null)
			{
				if (_isExporting)
				{
					foreach (TableCell c in e.Item.Cells)
					{
						c.HorizontalAlign = HorizontalAlign.Center;
						c.ForeColor = Color.FromName("White");
					}

					e.Item.Cells[2].Text = drv["DELIVERY_DATE"].ToString();
					e.Item.Cells[2].ForeColor = Color.FromName("Black");
				}

				string iStatusBanv = drv["BANV_STATUS"] is DBNull ? null : drv["BANV_STATUS"].ToString();

				if (iStatusBanv == EmailDeliveryStatus.Failed.ToString())
				{
					e.Item.Cells[3].BackColor = Color.FromName("Red");
				}
				else if (iStatusBanv == EmailDeliveryStatus.Delivered.ToString())
				{
					e.Item.Cells[3].BackColor = Color.FromName("Green");
				}

				string iStatusPerson = drv["PERSONAL_STATUS"] is DBNull ? null : drv["PERSONAL_STATUS"].ToString();

				if (iStatusPerson == EmailDeliveryStatus.Failed.ToString())
				{
					e.Item.Cells[4].BackColor = Color.FromName("Red");
				}
				else if (iStatusPerson == EmailDeliveryStatus.Delivered.ToString())
				{
					e.Item.Cells[4].BackColor = Color.FromName("Green");
				}

				string iStatusParent = drv["PARENT_STATUS"] is DBNull ? null : drv["PARENT_STATUS"].ToString();

				if (iStatusParent == EmailDeliveryStatus.Failed.ToString())
				{
					e.Item.Cells[5].BackColor = Color.FromName("Red");
				}
				else if (iStatusParent == EmailDeliveryStatus.Delivered.ToString())
				{
					e.Item.Cells[5].BackColor = Color.FromName("Green");
				}
			}
		}
	}

	protected override void Fill()
	{
		string message;

		decimal studentId;

		decimal.TryParse(Request.Params["studentId"], out studentId);

		DataTable dt = new DataLayer().GetStudentEmailHistory(PAConfiguration.ConnectionString, studentId, out message);

		rgStudentEmailHistory.DataSource = dt;

		if (dt != null && dt.Rows.Count > 0)
		{
			lblStudentName.Text = dt.Rows[0]["LASTFIRST"].ToString();
		}
	}

	/*protected void rgStudentEmailHistory_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
	{
		Fill();
	}
	*/

}